package com.example.mybook.net;

import android.os.Handler;
import android.os.Looper;
import android.util.TimeUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpUtils {
    private static OkHttpClient client;
    private static OkHttpClient sInstance = new OkHttpClient();
    private Handler mhandler = new Handler(Looper.getMainLooper());

    public static OkHttpClient getInstance(){
        if (sInstance==null){
            sInstance = new OkHttpClient();
            return sInstance;
        }else {
            return sInstance;
        }
    }

    public OkHttpUtils() {
        client = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS)
                .connectTimeout(5,TimeUnit.SECONDS)
                .build();
    }

    public void doget(String url,INetCallBack callBack){
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        executecallback(callBack, request);
    }

    public void dopost(String url, HashMap<String ,String> params, INetCallBack callBack){
        FormBody.Builder formBodybuilder = new FormBody.Builder();
               if (params!=null){
                   for (String param:params.keySet()){
                       formBodybuilder.add(param,params.get(param));
                   }
               }
        Request request = new Request.Builder()
                .url(url)
                .post(formBodybuilder.build())
                .build();
        executecallback(callBack, request);
    }

    public void dopostMutipart(String url, HashMap<String ,String> params, INetCallBack callBack){
        MultipartBody.Builder MutipartBodybuilder = new MultipartBody.Builder();
        MutipartBodybuilder.setType(MultipartBody.FORM);
        if (params!=null){
            for (String param:params.keySet()){
                MutipartBodybuilder.addFormDataPart(param,params.get(param));
            }
        }
        Request request = new Request.Builder()
                .url(url)
                .post(MutipartBodybuilder.build())
                .build();
        executecallback(callBack, request);
    }

    public void dopostjson(){

    }

    private void executecallback(INetCallBack callBack, Request request) {
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                mhandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailure(e);
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String jsonstr = null;

                try {
                    jsonstr =  request.body().toString();
                } catch (Exception e) {
                    mhandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callBack.onFailure(e);
                        }
                    });
                }
                String finalJsonstr = jsonstr;
                mhandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(finalJsonstr);
                    }
                });

            }
        });
    }
}
